package system_smtp_log_attachment_dao

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"time"
)

func (cls *SystemSmtpLogAttachment) SaveSystemSmtpLogAttachment(ctx context.Context, systemSmtpLogAttachment *model.SystemSmtpLogAttachment) errors.Error {
	if nil == systemSmtpLogAttachment {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSLA_NT.SSLA_NT.SSSLA_NT.150357 systemSmtpLogAttachment is nil")

		return errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.SSSLA_NT.170357", "异常的传入参数")
	}

	systemSmtpLogAttachment.UpdateTime = pointer.New(time.Now().Unix())
	if nil == systemSmtpLogAttachment.CreateTime || 0 == *systemSmtpLogAttachment.CreateTime {
		systemSmtpLogAttachment.CreateTime = pointer.New(time.Now().Unix())

		if err := cls.GetDb(ctx).Model(new(model.SystemSmtpLogAttachment)).Create(&systemSmtpLogAttachment).Error; nil != err {
			logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSLA_NT.SSLA_NT.SSSLA_NT.250357 create systemSmtpLogAttachment failed, err %v", err)

			return errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.SSSLA_NT.270357", "创建数据失败")
		}

		return nil
	}

	if err := cls.GetDb(ctx).Model(new(model.SystemSmtpLogAttachment)).Where("id = ?", systemSmtpLogAttachment.Id).Updates(&systemSmtpLogAttachment).Error; nil != err {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSLA_NT.SSLA_NT.SSSLA_NT.340357 save systemSmtpLogAttachment failed, err %v", err)

		return errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.SSSLA_NT.360357", "更新数据失败")
	}

	return nil
}
